package com.msun.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.entity.Result;
import com.msun.pojo.Sports;
import com.msun.service.SportsService;
import com.msun.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 99037
 */
@RestController
@RequestMapping({"/sports"})
public class SportsController {

    @Reference
    private SportsService sportsService;

    @RequestMapping({"/downLoad"})
    public Result downLoad(HttpServletResponse response, String filePath, HttpServletRequest request)
            throws Exception {
        String path = request.getSession().getServletContext().getRealPath("template");
        String fileName = new String("sports.xls".getBytes(), "iso-8859-1");

        response.setHeader("ContentType", request.getSession().getServletContext().getMimeType("sports.xls"));

        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);

        File file = new File(path, fileName);

        InputStream input = new FileInputStream(file);

        OutputStream out = response.getOutputStream();


        byte[] buff = new byte[1024];

        int index = 0;


        while ((index = input.read(buff)) != -1) {

            out.write(buff, 0, index);

            out.flush();
        }
        out.close();
        input.close();
        return null;
    }

    @RequestMapping({"/upload"})
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        System.out.println(excelFile);
        try {
            List<String[]> excel = POIUtils.readExcel(excelFile);
            List<Sports> list = new ArrayList();

            for (String[] sportss : excel) {
                Sports sports=new Sports();

                String id = sportss[0];
                sports.setId(Integer.parseInt(id));

                String code = sportss[1];
                sports.setCode(code);

                String name = sportss[2];
                sports.setName(name);

                String number = sportss[3];
                sports.setNumber(number);

                String time = sportss[4];
                sports.setTime(time);

                list.add(sports);
            }

            sportsService.add(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "批量导入食品数据失败,请检查是否重复导入");
        }

        return new Result(true, "批量导入食品数据成功");
    }

    @RequestMapping({"/findByPage"})
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {

        System.out.println(queryPageBean);

        Integer pageSize = queryPageBean.getPageSize();
        System.out.println(pageSize);

        System.out.println(queryPageBean.toString());

        PageResult list = sportsService.findAll(queryPageBean);
        return new PageResult(list.getTotal(), list.getRows());
    }

    @RequestMapping({"/add"})
    public Result add(@RequestBody Sports sports) {
        System.out.println(sports);
        try {
            sportsService.addSports(sports);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增运动失败");
        }
        return new Result(true, "新增运动成功");
    }

    @RequestMapping({"/findAll"})
    public Result findAll(Integer id) {
        try {
            List<Sports> list= sportsService.findAllSports(id);
            return new Result(true, "查询成功成功",list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败失败");
        }
    }


    @RequestMapping("/findSportsIdsByOrderId")
    public Result findSportsIdsByOrderId(Integer id) {
        try {
            List<Integer> list= sportsService.findSportsIdsByOrderId(id);
            return new Result(true, "查询成功成功",list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败失败");
        }
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            sportsService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "删除失败");
        }
        return  new Result(true, "删除成功");
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Sports sports){
        try{

            sportsService.edit(sports);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false,"删除失败" );
        }
        return  new Result(true, "删除成功");
    }

}

